package com.yike.user.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yike.common.tool.HttpsRequestUtil;
import com.yike.user.vo.ztsk.ZtskLoginVo;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

/**
 * @author Sakura
 * @description: 智通数控工具类
 * @date 2024/9/9 9:23
 */
@Component
@Log
public class ZtskUtil {

    private static final String SALT = "$qyzj2024";

    @Value("${ztsk.get-token-url}")
    private String ZTSK_GET_TOKEN_URL;

    @Value("${ztsk.del-token-url}")
    private String ZTSK_DEL_TOKEN_URL;

    @Value("${ztsk.un_bind-dev-url}")
    private String UN_BIND_DEV_URL;

    //private static final String ZTSK_GET_TOKEN_URL = "http://192.168.31.207:8083/phoneAuth/getToken";

    public ZtskLoginVo login(String moblie) {
        log.info("登录智通数控系统");
        String hashedPassword = DigestUtils.md5DigestAsHex((moblie + SALT).getBytes());
        // 拼接请求参数
        StringBuilder strUrl = new StringBuilder();
        strUrl.append(ZTSK_GET_TOKEN_URL)
                .append("?number=").append(moblie)
                .append("&password=").append(hashedPassword);

        ZtskLoginVo ztskLoginVo = new ZtskLoginVo();
        try {
            log.info("【智通数控登录】：url " + strUrl);
            String resultStr = HttpsRequestUtil.sendGetRequest(strUrl.toString());
            log.info("【智通数控登录】：" + resultStr);
            JSONObject json = JSONObject.parseObject(resultStr);
            if (json.getInteger("flag") != null && json.getInteger("flag") == 1) {
                ztskLoginVo = JSON.parseObject(json.getString("data"), ZtskLoginVo.class);
            }
        } catch (Exception e) {
            log.info("【智通数控登录】：异常：" + e.getMessage());
            e.printStackTrace();
        }

        return ztskLoginVo;
    }

    public ZtskLoginVo logout(String moblie) {
        log.info("退出智通数控系统");
        String hashedPassword = DigestUtils.md5DigestAsHex((moblie + SALT).getBytes());
        // 拼接请求参数
        StringBuilder strUrl = new StringBuilder();
        strUrl.append(ZTSK_DEL_TOKEN_URL)
                .append("?number=").append(moblie)
                .append("&password=").append(hashedPassword);

        ZtskLoginVo ztskLoginVo = new ZtskLoginVo();
        try {
            log.info("【退出智通数控】：url " + strUrl);
            String resultStr = HttpsRequestUtil.sendGetRequest(strUrl.toString());
            log.info("【退出智通数控】：" + resultStr);
            JSONObject json = JSONObject.parseObject(resultStr);
            if (json.getInteger("flag") != null && json.getInteger("flag") == 1) {
                ztskLoginVo = JSON.parseObject(json.getString("data"), ZtskLoginVo.class);
            }
        } catch (Exception e) {
            log.info("【退出智通数控】：异常：" + e.getMessage());
            e.printStackTrace();
        }

        return ztskLoginVo;
    }

    public ZtskLoginVo unbindDev(String devCode, Long userId, String token) {
        log.info("解绑智通数控系统设备");
        // 拼接请求参数
        StringBuilder strUrl = new StringBuilder();
        strUrl.append(UN_BIND_DEV_URL)
                .append("?devCode=").append(devCode)
                .append("&userId=").append(userId);

        ZtskLoginVo ztskLoginVo = new ZtskLoginVo();
        try {
            log.info("【解绑智通数控系统设备】：url " + strUrl);
            String resultStr = HttpsRequestUtil.sendGetRequest(strUrl.toString(), token);
            log.info("【解绑智通数控系统设备】：" + resultStr);
            JSONObject json = JSONObject.parseObject(resultStr);
            if (json.getInteger("flag") != null && json.getInteger("flag") == 1) {
                ztskLoginVo = JSON.parseObject(json.getString("data"), ZtskLoginVo.class);
            }
        } catch (Exception e) {
            log.info("【解绑智通数控系统设备】：异常：" + e.getMessage());
            e.printStackTrace();
        }

        return ztskLoginVo;
    }
}
